package br.edu.univas.si.engs.view;

import java.awt.Frame;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Date;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;

import javax.swing.BorderFactory;
import javax.swing.ComboBoxModel;
import javax.swing.DefaultComboBoxModel;
import javax.swing.GroupLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JComponent;
import javax.swing.JDialog;
import javax.swing.JFormattedTextField;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.border.BevelBorder;

import br.edu.univas.si.engs.controller.ControllerCadastro;
import br.edu.univas.si.engs.model.AddressTO;
import br.edu.univas.si.engs.model.CarTO;
import br.edu.univas.si.engs.model.ClientTO;

/**
 * This code was edited or generated using CloudGarden's Jigloo SWT/Swing GUI
 * Builder, which is free for non-commercial use. If Jigloo is being used
 * commercially (ie, by a corporation, company or business for any purpose
 * whatever) then you should purchase a license for each developer using Jigloo.
 * Please visit www.cloudgarden.com for details. Use of Jigloo implies
 * acceptance of these licensing terms. A COMMERCIAL LICENSE HAS NOT BEEN
 * PURCHASED FOR THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED LEGALLY FOR
 * ANY CORPORATE OR COMMERCIAL PURPOSE.
 */
public class DialogClienteDados extends JDialog {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final Frame Frame = null;

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager
					.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsClassicLookAndFeel");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JPanel CadastroDadosPessoaisPanel;
	private JLabel NomeLabel;
	private JButton CancelButton;
	private JButton NextButton;
	private JComboBox StatusComboBox;
	private JLabel StatusLabel;
	private JTextField DataCadTextField;
	private JLabel DataCadLabel;
	private JTextField CelularTextField;
	private JLabel CelularLabel;
	private JLabel EmailLabel;
	private JTextField TelefoneTextField;
	private JLabel TelefoneLabel;
	private JTextField DataNascTextField;
	private JLabel DataNascLabel;
	private JTextField EmailTextField;
	private JTextField RgTextField;
	private JLabel RgLabel;
	private JTextField CpfField;
	private JLabel CpfLabel;
	private JTextField NomeTextField;
	Boolean statusAddUpatde = false;
	int idEndereco;
	ControllerCadastro cc = new ControllerCadastro();
	/**
	 * Auto-generated main method to display this JDialog
	 */

	public DialogClienteDados() {
		super(Frame, "Cadastro", true);
		initGUI();
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		setLocationRelativeTo(null);

	}

	private void initGUI() {
		try {
			GroupLayout thisLayout = new GroupLayout(
					(JComponent) getContentPane());
			getContentPane().setLayout(thisLayout);
			this.setTitle("Dados Pessoais");
			{
				CadastroDadosPessoaisPanel = new JPanel();
				GroupLayout CadastroDadosPessoaisPanelLayout = new GroupLayout(
						(JComponent) CadastroDadosPessoaisPanel);
				CadastroDadosPessoaisPanel
						.setLayout(CadastroDadosPessoaisPanelLayout);
				CadastroDadosPessoaisPanel.setBorder(BorderFactory
						.createBevelBorder(BevelBorder.LOWERED));
				{
					NomeLabel = new JLabel();
					NomeLabel.setText("Nome:");
				}
				{
					TelefoneLabel = new JLabel();
					TelefoneLabel.setText("Telefone:");
				}
				{
					TelefoneTextField = new JTextField();
					TelefoneTextField.setText("3534238531");
				}
				{
					CelularLabel = new JLabel();
					CelularLabel.setText("Celular:");
				}
				{
					CelularTextField = new JTextField();
					CelularTextField.setText("3588144573");
				}
				{
					DataCadLabel = new JLabel();
					DataCadLabel.setText("Data Cadastro:");
				}
				{
					DataCadTextField = new JTextField();
					DataCadTextField.setText("22/02/1985");
				}
				{
					StatusLabel = new JLabel();
					StatusLabel.setText("Status:");
				}
				{
					ComboBoxModel StatusComboBoxModel = new DefaultComboBoxModel(
							new String[] { "Ativo", "Inativo" });
					StatusComboBox = new JComboBox();
					StatusComboBox.setModel(StatusComboBoxModel);
					StatusComboBox.setEditable(true);
					StatusComboBox.setBorder(BorderFactory
							.createBevelBorder(BevelBorder.LOWERED));
				}
				{
					NomeTextField = new JTextField();
					NomeTextField.setText("Oliveira");
				}
				{
					CpfLabel = new JLabel();
					CpfLabel.setText("CPF:");
				}
				{
					CpfField = new JTextField();
					CpfField.setText("06316761641");
				}
				{
					RgLabel = new JLabel();
					RgLabel.setText("RG:");
				}
				{
					RgTextField = new JTextField();
					RgTextField.setText("13530984");
				}
				{
					EmailLabel = new JLabel();
					EmailLabel.setText("E-mail:");
				}
				{
					EmailTextField = new JTextField();
					EmailTextField.setText("andrejoseo@yahoo.com.br");
				}
				{
					DataNascLabel = new JLabel();
					DataNascLabel.setText("Data Nascimento:");
				}
				{
					DataNascTextField = new JTextField();
					DataNascTextField.setText("22/02/1985");
				}
				CadastroDadosPessoaisPanelLayout
						.setHorizontalGroup(CadastroDadosPessoaisPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										CadastroDadosPessoaisPanelLayout
												.createParallelGroup()
												.addGroup(
														CadastroDadosPessoaisPanelLayout
																.createSequentialGroup()
																.addGroup(
																		CadastroDadosPessoaisPanelLayout
																				.createParallelGroup()
																				.addComponent(
																						CpfLabel,
																						GroupLayout.Alignment.LEADING,
																						GroupLayout.PREFERRED_SIZE,
																						34,
																						GroupLayout.PREFERRED_SIZE)
																				.addComponent(
																						NomeLabel,
																						GroupLayout.Alignment.LEADING,
																						GroupLayout.PREFERRED_SIZE,
																						34,
																						GroupLayout.PREFERRED_SIZE))
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.RELATED)
																.addGroup(
																		CadastroDadosPessoaisPanelLayout
																				.createParallelGroup()
																				.addGroup(
																						GroupLayout.Alignment.LEADING,
																						CadastroDadosPessoaisPanelLayout
																								.createSequentialGroup()
																								.addComponent(
																										CpfField,
																										GroupLayout.PREFERRED_SIZE,
																										85,
																										GroupLayout.PREFERRED_SIZE)
																								.addPreferredGap(
																										LayoutStyle.ComponentPlacement.RELATED)
																								.addComponent(
																										RgLabel,
																										GroupLayout.PREFERRED_SIZE,
																										24,
																										GroupLayout.PREFERRED_SIZE)
																								.addComponent(
																										RgTextField,
																										GroupLayout.PREFERRED_SIZE,
																										87,
																										GroupLayout.PREFERRED_SIZE)
																								.addGroup(
																										CadastroDadosPessoaisPanelLayout
																												.createParallelGroup()
																												.addGroup(
																														GroupLayout.Alignment.LEADING,
																														CadastroDadosPessoaisPanelLayout
																																.createSequentialGroup()
																																.addGroup(
																																		CadastroDadosPessoaisPanelLayout
																																				.createParallelGroup()
																																				.addGroup(
																																						GroupLayout.Alignment.LEADING,
																																						CadastroDadosPessoaisPanelLayout
																																								.createSequentialGroup()
																																								.addComponent(
																																										TelefoneTextField,
																																										GroupLayout.PREFERRED_SIZE,
																																										83,
																																										GroupLayout.PREFERRED_SIZE)
																																								.addPreferredGap(
																																										LayoutStyle.ComponentPlacement.RELATED)
																																								.addComponent(
																																										CelularLabel,
																																										GroupLayout.PREFERRED_SIZE,
																																										46,
																																										GroupLayout.PREFERRED_SIZE))
																																				.addGroup(
																																						GroupLayout.Alignment.LEADING,
																																						CadastroDadosPessoaisPanelLayout
																																								.createSequentialGroup()
																																								.addComponent(
																																										StatusComboBox,
																																										GroupLayout.PREFERRED_SIZE,
																																										95,
																																										GroupLayout.PREFERRED_SIZE)
																																								.addGap(38)))
																																.addComponent(
																																		CelularTextField,
																																		GroupLayout.PREFERRED_SIZE,
																																		90,
																																		GroupLayout.PREFERRED_SIZE)
																																.addGap(0,
																																		124,
																																		Short.MAX_VALUE))
																												.addGroup(
																														GroupLayout.Alignment.LEADING,
																														CadastroDadosPessoaisPanelLayout
																																.createSequentialGroup()
																																.addGap(16)
																																.addComponent(
																																		EmailLabel,
																																		GroupLayout.PREFERRED_SIZE,
																																		40,
																																		GroupLayout.PREFERRED_SIZE)
																																.addPreferredGap(
																																		LayoutStyle.ComponentPlacement.UNRELATED)
																																.addComponent(
																																		EmailTextField,
																																		GroupLayout.PREFERRED_SIZE,
																																		281,
																																		GroupLayout.PREFERRED_SIZE)
																																.addGap(0,
																																		0,
																																		Short.MAX_VALUE))))
																				.addGroup(
																						CadastroDadosPessoaisPanelLayout
																								.createSequentialGroup()
																								.addComponent(
																										NomeTextField,
																										GroupLayout.PREFERRED_SIZE,
																										547,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(0,
																										0,
																										Short.MAX_VALUE))))
												.addGroup(
														CadastroDadosPessoaisPanelLayout
																.createSequentialGroup()
																.addGroup(
																		CadastroDadosPessoaisPanelLayout
																				.createParallelGroup()
																				.addGroup(
																						GroupLayout.Alignment.LEADING,
																						CadastroDadosPessoaisPanelLayout
																								.createSequentialGroup()
																								.addComponent(
																										DataCadLabel,
																										GroupLayout.PREFERRED_SIZE,
																										81,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(12)
																								.addGroup(
																										CadastroDadosPessoaisPanelLayout
																												.createParallelGroup()
																												.addComponent(
																														DataNascTextField,
																														GroupLayout.Alignment.LEADING,
																														GroupLayout.PREFERRED_SIZE,
																														79,
																														GroupLayout.PREFERRED_SIZE)
																												.addComponent(
																														DataCadTextField,
																														GroupLayout.Alignment.LEADING,
																														GroupLayout.PREFERRED_SIZE,
																														79,
																														GroupLayout.PREFERRED_SIZE)))
																				.addGroup(
																						GroupLayout.Alignment.LEADING,
																						CadastroDadosPessoaisPanelLayout
																								.createSequentialGroup()
																								.addComponent(
																										DataNascLabel,
																										GroupLayout.PREFERRED_SIZE,
																										96,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(76)))
																.addPreferredGap(
																		LayoutStyle.ComponentPlacement.UNRELATED)
																.addGroup(
																		CadastroDadosPessoaisPanelLayout
																				.createParallelGroup()
																				.addGroup(
																						CadastroDadosPessoaisPanelLayout
																								.createSequentialGroup()
																								.addComponent(
																										TelefoneLabel,
																										GroupLayout.PREFERRED_SIZE,
																										53,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(0,
																										0,
																										Short.MAX_VALUE))
																				.addGroup(
																						GroupLayout.Alignment.LEADING,
																						CadastroDadosPessoaisPanelLayout
																								.createSequentialGroup()
																								.addComponent(
																										StatusLabel,
																										GroupLayout.PREFERRED_SIZE,
																										47,
																										GroupLayout.PREFERRED_SIZE)
																								.addGap(0,
																										6,
																										Short.MAX_VALUE)))
																.addGap(350)))
								.addContainerGap(54, 54));
				CadastroDadosPessoaisPanelLayout
						.setVerticalGroup(CadastroDadosPessoaisPanelLayout
								.createSequentialGroup()
								.addContainerGap()
								.addGroup(
										CadastroDadosPessoaisPanelLayout
												.createParallelGroup(
														GroupLayout.Alignment.BASELINE)
												.addComponent(
														NomeTextField,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														20,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														NomeLabel,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										CadastroDadosPessoaisPanelLayout
												.createParallelGroup()
												.addGroup(
														GroupLayout.Alignment.LEADING,
														CadastroDadosPessoaisPanelLayout
																.createParallelGroup(
																		GroupLayout.Alignment.BASELINE)
																.addComponent(
																		EmailTextField,
																		GroupLayout.Alignment.BASELINE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		EmailLabel,
																		GroupLayout.Alignment.BASELINE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		CpfField,
																		GroupLayout.Alignment.BASELINE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		RgLabel,
																		GroupLayout.Alignment.BASELINE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE)
																.addComponent(
																		RgTextField,
																		GroupLayout.Alignment.BASELINE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE))
												.addGroup(
														GroupLayout.Alignment.LEADING,
														CadastroDadosPessoaisPanelLayout
																.createSequentialGroup()
																.addGap(7)
																.addComponent(
																		CpfLabel,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE,
																		GroupLayout.PREFERRED_SIZE)))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										CadastroDadosPessoaisPanelLayout
												.createParallelGroup(
														GroupLayout.Alignment.BASELINE)
												.addComponent(
														TelefoneTextField,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														CelularLabel,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														CelularTextField,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														TelefoneLabel,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														DataNascLabel,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														DataNascTextField,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addPreferredGap(
										LayoutStyle.ComponentPlacement.RELATED)
								.addGroup(
										CadastroDadosPessoaisPanelLayout
												.createParallelGroup(
														GroupLayout.Alignment.BASELINE)
												.addComponent(
														StatusComboBox,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														21,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														StatusLabel,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														DataCadTextField,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE)
												.addComponent(
														DataCadLabel,
														GroupLayout.Alignment.BASELINE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE,
														GroupLayout.PREFERRED_SIZE))
								.addContainerGap(34, 34));
			}
			{
				NextButton = new JButton();
				NextButton.setText("Avancar");
				NextButton.setIcon(new ImageIcon(getClass().getClassLoader()
						.getResource("images/Next1.png")));
				NextButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {

						getCadastroDados();
						setVisible(false);

					}
				});
			}
			{
				CancelButton = new JButton();
				CancelButton.setText("Cancelar");
				CancelButton.setIcon(new ImageIcon(getClass().getClassLoader()
						.getResource("images/cancel.png")));
				CancelButton.setFont(new java.awt.Font("Tahoma", 0, 10));
				CancelButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						dispose();

					}
				});
			}
			thisLayout.setVerticalGroup(thisLayout
					.createSequentialGroup()
					.addContainerGap()
					.addComponent(CadastroDadosPessoaisPanel,
							GroupLayout.PREFERRED_SIZE, 149,
							GroupLayout.PREFERRED_SIZE)
					.addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 0,
							Short.MAX_VALUE)
					.addGroup(
							thisLayout
									.createParallelGroup(
											GroupLayout.Alignment.BASELINE)
									.addComponent(NextButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 29,
											GroupLayout.PREFERRED_SIZE)
									.addComponent(CancelButton,
											GroupLayout.Alignment.BASELINE,
											GroupLayout.PREFERRED_SIZE, 23,
											GroupLayout.PREFERRED_SIZE))
					.addContainerGap(16, 16));
			thisLayout
					.setHorizontalGroup(thisLayout
							.createSequentialGroup()
							.addContainerGap()
							.addGroup(
									thisLayout
											.createParallelGroup()
											.addComponent(
													CadastroDadosPessoaisPanel,
													GroupLayout.Alignment.LEADING,
													0, 611, Short.MAX_VALUE)
											.addGroup(
													GroupLayout.Alignment.LEADING,
													thisLayout
															.createSequentialGroup()
															.addGap(331)
															.addComponent(
																	CancelButton,
																	GroupLayout.PREFERRED_SIZE,
																	96,
																	GroupLayout.PREFERRED_SIZE)
															.addGap(16)
															.addComponent(
																	NextButton,
																	GroupLayout.PREFERRED_SIZE,
																	153,
																	GroupLayout.PREFERRED_SIZE)
															.addGap(0,
																	15,
																	Short.MAX_VALUE)))
							.addContainerGap());
			this.setSize(639, 245);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void getCadastroDados() {
		ClientTO pessoais = new ClientTO();
		pessoais.setNome(NomeTextField.getText());
		pessoais.setCpf(CpfField.getText());
		pessoais.setRg(RgTextField.getText());
		pessoais.setTelefone(TelefoneTextField.getText());
		pessoais.setEmail(EmailTextField.getText());
		pessoais.setCelular(CelularTextField.getText());
		pessoais.setDataNasc(DataNascTextField.getText());
		pessoais.setDataCad(DataCadTextField.getText());
		pessoais.setStatus(getStatusCombobox());
		

		DialogClienteAdress adress = new DialogClienteAdress();
		 
		 AddressTO adres=null;
		if (statusAddUpatde == false)
			adress.getCadastroAdress(pessoais,false);
		
		else{
			System.out.println("getCadastroDados() "+pessoais.getNome());
			adres = cc.editarAdress(idEndereco);
			pessoais.setEndereco(idEndereco);
			adress.getCadastroAdressUpdate(pessoais, adres);
		}
		adress.setVisible(true);
	}

	public Boolean getStatusCombobox() {
		if (StatusComboBox.getSelectedItem().toString().equals("Ativo"))
			return true;
		else
			return false;

	}

	public String getConvertStatus(Boolean status) {
		String sta;
		if (status == true)
			sta = "Ativo";
		else
			sta = "Inativo";

		return sta;
	}

	public void getEditarDados(ClientTO client) {

		NomeTextField.setText(client.getNome());
		CpfField.setText(client.getCpf());
		RgTextField.setText(client.getRg());
		TelefoneTextField.setText(client.getTelefone());
		EmailTextField.setText(client.getEmail());
		CelularTextField.setText(client.getCelular());
		DataNascTextField.setText(client.getDataNasc());
		DataCadTextField.setText(client.getDataCad());
		StatusComboBox.setSelectedItem(getConvertStatus(client.getStatus()));
		idEndereco = client.getEndereco();
		
		getVerificaAlterAdd();

	

	}

	public void getVerificaAlterAdd() {
		statusAddUpatde = true;
	}

}
